/*
 * Copyright (c) 2010 CCX(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCX(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCX(China) Co.,Ltd.
 *
 * Author: zhnb
 * Date: 2011-4-21 下午02:09:38
 */

package com.ccx.memc;

import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.Date;

import org.dom4j.DocumentException;

import com.danga.MemCached.MemCachedClient;
import com.danga.MemCached.SockIOPool;

/**
 * 内存缓冲对象
 * 
 */
public final class MemcachedOpr implements Serializable {

    private static final long serialVersionUID = -3817533683988206950L;

    // 客户端配置
    private static MemCachedClient mcc = new MemCachedClient();
    private static MemcachedOpr memc = new MemcachedOpr();

    // 设置与缓存服务器的连接池
    static {

        MemcConfigInfo mci = null;

        // 获取配置信息对象
        try {
            mci = MemcConfigLoader.init();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (DocumentException e) {
            e.printStackTrace();
        }

        // 如果配置信息为空，直接返回。
        if (mci == null) {
            throw new RuntimeException("init faild. Cause by memcached inital.");
        }

        // 获取socke连接池的实例对象
        SockIOPool pool = SockIOPool.getInstance();

        // 设置服务器信息
        pool.setServers(mci.getServers());
        pool.setWeights(mci.getWeights());

        // 设置初始连接数、最小和最大连接数以及最大处理时间
        pool.setInitConn(mci.getInitConn());
        pool.setMinConn(mci.getMinConn());
        pool.setMaxConn(mci.getMaxConn());
        pool.setMaxIdle(mci.getMaxIdle());

        // 设置主线程的睡眠时间
        pool.setMaintSleep(mci.getMaintSleep());

        // 设置TCP的参数，连接超时等
        pool.setNagle(mci.isNagle());
        pool.setSocketTO(mci.getSocketTO());
        pool.setSocketConnectTO(mci.getSocketConnectTO());

        // 初始化连接池
        pool.initialize();

        // 压缩设置，超过指定大小（单位为K）的数据都会被压缩
        mcc.setCompressEnable(mci.isCompressEnable());
        mcc.setCompressThreshold(mci.getCompressThreshold());
    }

    private MemcachedOpr() {
    }

    /**
     * 获取缓存对象
     * 
     * @return
     */
    public static MemcachedOpr getInstance() {
        return memc;
    }

    // 操作方法列表
    public boolean add(String key, Object value) {
        return mcc.add(key, value);
    }

    public boolean add(String key, Object value, Date expiry) {
        return mcc.add(key, value, expiry);
    }

    public boolean replace(String key, Object value) {
        return mcc.replace(key, value);
    }

    public boolean replace(String key, Object value, Date expiry) {
        return mcc.replace(key, value, expiry);
    }

    public Object get(String key) {
        Object obj = mcc.get(key);
        // 如果内存缓存中不存在，返回NULL，否则返回该值序列的字符串表达。
        return obj;
    }
}
